<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Info extends CI_Controller {

    /**
     * Crawl Weibo Content And Info By Keyword
     *
     */
    const SINA          = 'sina';
    const YUNYUN        = 'yunyun';
    const NETEASE       = 'netease';
    const SOHU          = 'sohu';
    const TENCENT       = 'tencent';
    const PLATFORM      = 'platform';
    const ADMIN         = 'soften';
    const ERROR_TAB     = 'error';
    const LOG_LEVEL_ERROR = 'error';
    const LOG_LEVEL_WARING = 'waring';
    const LOG_LEVEL_NOTICE = 'notice';
    public $_log_dir    = 'keyword';
    public $_result_dir = 'result';
    public $_return_data;

    public function __construct(){
        parent::__construct();
        $this->load->helper('MY_auth_verification');
        $this->load->library('error_code_library');
        $this->load->library('crawl_abstract');
        //$this->load->database();
        $this->_return_data = $this->config->item('return_data');
    }

    public function index(){
		$this->get_info();
	}

    public function get_info() {
    	
    	$this->load->database();
    	
    	//$data = json_decode($this->input->post('data'), TRUE);
    	$data = $GLOBALS['HTTP_RAW_POST_DATA'] ? json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true) : array();
    	if($data){
    		$_POST['token'] = $data['token'];
    		$_POST['platform'] = $data['platform'];
    		$_POST['url'] = json_encode($data['url']);
    	}
    	
        $auth_verification_result = authentication($this, $this->input->post());
        if(is_array($auth_verification_result) && array_key_exists(self::ERROR_TAB,$auth_verification_result)) {
            echo json_encode($auth_verification_result);
            exit;
        } else {
            $this->crawl();
        }
    }
    
    /**
     * 批量更新数据
     * 
     * 1、接收数据，2、插入队列，3、后台进程更新存redis的set中，4、主进程判断set个数返回数据
     */
    public function batch_get_info() {
    	
    	$this->load->database();
    	
    	//$data = json_decode($this->input->post('data'), TRUE);
    	$data = $GLOBALS['HTTP_RAW_POST_DATA'] ? json_decode($GLOBALS['HTTP_RAW_POST_DATA'], true) : array();
    	if($data){
    		$_POST['token'] = $data['token'];
    		$_POST['platform'] = $data['platform'];
    		$_POST['url'] = json_encode($data['url']);
    	}
    	 
    	$auth_verification_result = authentication($this, $this->input->post());
    	if(is_array($auth_verification_result) && array_key_exists(self::ERROR_TAB,$auth_verification_result)) {
    		echo json_encode($auth_verification_result);
    		exit;
    	} else {
    		$result_url = json_decode($this->input->post('url'), TRUE);
    		
    		$result_url = array_unique($result_url);
    		
    		//判断接收的URL如果大于1000 则返回前1000，并且截取出URL的最后参数
    		if( is_array($result_url) && !empty($result_url) && count($result_url)< 1000){
    			$result_url = $result_url;
    		}else{
    			$result_url = array_slice($result_url, 0, 1000);
    		}
    		
    		$this->load->driver('cache', array('adapter' => 'redis'));
    		//$this->cache->select(1);
    		
    		$queue_num = 0;
    		$html_crawl_result = array();
    		
    		//每次随机一个key的名字，防止并发访问key冲突
    		$set_name = "set_batch_info_" . rand(1000, 9999);
    		//$set_name = "set_batch_info_0000";
    		
    		//提交微博ID至队列，并记录个数
    		foreach ($result_url as $result_id){
    			$queue_value = json_encode(array("set_name"=>$set_name, "result_id"=>$result_id));
    			$this->cache->rpush("batch_get_info", $queue_value);
    			$queue_num ++;
    		}
    		//echo $queue_num;
    		$i = 1;
    		while ($i<30){
    			
    			$i ++;
    			
    			//判断redis的set集合长度
    			$scard = $this->cache->scard($set_name);
    			
    			if($scard != $queue_num){
    				sleep(1);
    				continue;
    			}
    			
    			//获取set的值
    			$smembers = $this->cache->smembers($set_name);
    			foreach ($smembers as $smember){
    				$html_crawl_result[] = json_decode($smember, TRUE);
    			}
    			
    			//删除set
    			$this->cache->delete($set_name);
    			
    			break;
    		}
    		//print_r($html_crawl_result);
    		//返回结果
    		$this->_return_data['data'] = $html_crawl_result;
    		$this->load->library('crawl_keyword_library');
    		$result_string = json_encode($this->crawl_keyword_library->return_utf8_code($this->_return_data));
    		echo $result_string;
    	}
    }
    
    public function crontab_crawl(){
    	
    	$_POST['platform'] = "sina";
    	
    	$this->load->driver('cache', array('adapter' => 'redis'));
    	//$this->cache->select(1);
    	
    	$queue_value = $this->cache->lpop("batch_get_info");
    	
    	if(empty($queue_value)){
    		echo "sleep 3 \n";
    		sleep(3);
    		return false;
    	}
    	
    	$this->load->database();
    	
    	echo "crontab_crawl ++";
    	
    	$queue = json_decode($queue_value, TRUE);
    	
    	$set_name = $queue['set_name'];
    	$result_id = $queue['result_id'];
    	
    	$this->load->library('crawl_keyword_library');
    	$this->load->library('Crawl_sina_info_from_phone_library');
    	
		$result_id_n = $result_id . "?";
        $ru = '|http:\/\/weibo.com/\d+/(.*?)\?|ims';
        preg_match_all($ru, $result_id_n, $data_url, PREG_PATTERN_ORDER);
        	
        if($data_url[1]){
        	$preg_url = $data_url[1][0];
        	$result_data = $this->crawl_sina_info_from_phone_library->crawl(true, $preg_url);
        }
        $result_data['url'] = $result_id;
        
        $rs = $this->cache->sadd($set_name, json_encode($result_data));
        
        echo ($rs ? "ok" : "no") . "\n";
    }

    private function crawl() {
        $this->load->library('crawl_keyword_library');

		$crawl_history = null;//$this->crawl_keyword_library->find_in_cache();

        $this->_log_dir = $this->_log_dir.'/'.trim($this->input->post('platform'));
        $this->_result_dir = $this->_log_dir.'/'.$this->_result_dir;

        if(is_null($crawl_history)) {
            log_message(self::LOG_LEVEL_NOTICE, 'Crawl BY '.json_encode($this->input->post()), FALSE, $this->_log_dir);
            $platform = trim($this->input->post(self::PLATFORM));
            $soften = trim($this->input->post("soften"));
            
            $html = trim($this->input->post("html"));

            if($platform == self::SINA) {
            	$result = $this->sina_crawl();
            } else {
                $result = json_encode($this->error_code_library->getError('system', 'params', 'no_platform', $platform));
                log_message(self::LOG_LEVEL_ERROR, 'NO Result: '.$result, FALSE, $this->_log_dir);
                echo $result;
            }

            if(is_null($result) || array_key_exists(self::ERROR_TAB, $result) == FALSE) {
                if(array_key_exists('data', $result)) {
                    $result_string = json_encode($this->_return_data);
                } else {
                    $this->_return_data['data'] = $result;
                    $result_string = json_encode($this->crawl_keyword_library->return_utf8_code($this->_return_data));
                    //不用缓存了
                    //$this->crawl_keyword_library->save_crawl_result($result_string);
                }

                log_message(self::LOG_LEVEL_NOTICE, 'Return Result Crawl String: '.$result_string, FALSE, $this->_result_dir);

                echo $result_string;
            } else {
            	//如果是soften自己使用的..则直接返回原始错误信息
                if($this->input->post(self::ADMIN) === FALSE  && $this->input->get(self::ADMIN) === FALSE) {
                    $this->_return_data['error'] = $this->error_code_library->getError('system', 'common', 'failed');
                } else {
                    $this->_return_data['error'] = $result;
                }
                log_message(self::LOG_LEVEL_ERROR, 'Crawl Error: '.json_encode($this->crawl_keyword_library->return_utf8_code($this->_return_data)), FALSE, $this->_log_dir);
                echo json_encode($this->_return_data);
            }
        } else {
            log_message(self::LOG_LEVEL_NOTICE, 'Return Crawl String From Cache: '.$crawl_history, FALSE, $this->_result_dir);
            echo $crawl_history;
        }
    }

    private function sina_crawl() {
        //++++++++++++
        $this->load->library('Crawl_sina_info_from_phone_library');
        
        $result_url = json_decode($this->input->post('url'), TRUE);
        
		//判断接收的URL如果大于20 则返回前20，并且截取出URL的最后参数
        if( is_array($result_url) && !empty($result_url) && count($result_url)< 20){
			$result_url = $result_url;
        }else{
			$result_url = array_slice($result_url, 0, 20);
        }
        
        $html_crawl_result = array();
        foreach ($result_url as $result_id){
        	$result_id = $result_id . "?";
        	$ru = '|http:\/\/weibo.com/\d+/(.*?)\?|ims';
        	preg_match_all($ru, $result_id, $data_url, PREG_PATTERN_ORDER);
        	
        	if($data_url[1]){
        		$preg_url = $data_url[1][0];
        		$result_data = $this->crawl_sina_info_from_phone_library->crawl(true, $preg_url);
        		$result_data['url'] = $result_id;
        		$html_crawl_result[] = $result_data;
        	}
        	
        }
        
        if(array_key_exists(self::ERROR_TAB, $html_crawl_result) === FALSE && $html_crawl_result != FALSE) {
            log_message(self::LOG_LEVEL_NOTICE, 'Crawl From Sina HTML Finish And Result Is: '.json_encode($this->crawl_keyword_library->return_utf8_code($html_crawl_result)), FALSE, $this->_result_dir);
            return $html_crawl_result;
        } else if(array_key_exists(self::ERROR_TAB, $html_crawl_result) && FALSE == $this->crawl_abstract->not_illegal_error($html_crawl_result)) {
            return $html_crawl_result;
        }else {
            log_message(self::LOG_LEVEL_ERROR, 'Crawl From Sina HTML Failed: '.json_encode($this->crawl_keyword_library->return_utf8_code($html_crawl_result)), FALSE, $this->_log_dir);
           
            return $html_crawl_result;
        }
    }

    public function test() {
        $this->load->helper('url');
        $this->load->library('table');

        $post_data = $this->input->post();
        
        if($post_data['password'] != "112358s"){
        	echo "hehe";
        	exit;
        }
        $post_data['platform'] = "sina";
        $post_data['uid'] = $this->input->post('uid') !== FALSE ? trim($this->input->post('uid')):"3970734335";
        $post_data['method'] = "POST";
        $post_data['token'] = "9f7ffa5b38e13b1843eeffcc3ba06565";
        $post_data['page']  = $this->input->post('page') !== FALSE ? trim($this->input->post('page')) : 1;
        $post_data['soften'] = "yes";

        //测试接口..添加调试信息
        if($this->input->post('soften_admin_debug')) {
        	$soften_admin_debug = true;
        	$post_data['soften_admin_debug'] = $soften_admin_debug;
        }

        //$post_data = http_build_query($post_data);
        if($this->config->item('test_use_proxy') == TRUE) {
            $url= $this->config->item('test_proxy_add')."users/index";
        } else {
            $url= site_url("users/index");
        }

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $result = curl_exec($ch);
        if ($result === FALSE) {
            echo "CURL Error In Test Connect API: " . curl_error($ch). " TO ".$url;
        }
        curl_close($ch);

        //测试接口..添加调试信息
        if($soften_admin_debug){
        	var_dump($result);
        	return ;
        }

        $wbfield = array(
            '微博ID', '微博信息内容', '微博创建时间', '微博来源', '转', '评', '好', '转载的微博ID', '转载的微博用户ID', '转载的微博用户名', '详情URL',
            '缩略图片地址', '中等尺寸图片地址', '原始图片地址', '增加时间', '渠道ID抓取渠道', '用户ID', '用户空间URL', '昵称', '描述', '头像', '粉丝数', '关注数', '是否加V'
        );

        if(is_null($result) ) {
            echo '无数据';
        } else {
            $result_data = json_decode($result, TRUE);
            if(is_null($result_data)) {
                echo '返回结果解析失败';die;
            }

            if(array_key_exists(self::ERROR_TAB, $result_data) && !empty($result_data[self::ERROR_TAB])) {
                echo '抓取错误<br/>';
                var_dump($result_data);
            } elseif(array_key_exists('data', $result_data) && is_null($result_data['data'])) {
                echo '无数据<br/>';
                var_dump($result_data);
            }else {
                echo '<head><meta charset="utf-8"><title>'.$post_data['platform'].'---'.$post_data['word'].'---'.$post_data['page'].'</title><style>*{font-size:12px}</style></head>';
                $tmpl = array ( 'table_open'  => '<table border="1" cellpadding="0" cellspacing="0" class="mytable">' );
                echo count($result_data['data']).'条记录';
                $this->table->set_template($tmpl);
                $this->table->set_heading($wbfield);
                foreach($result_data['data'] as $row) {
                	
                	$row = $row['mblog'];
                	
                    $data = array();
                    $data[] = $row['id'];
                    $data[] = $row['text'];
                    $data[] = $row['created_timestamp'];
                    $data[] = $row['source'];
                    $data[] = $row['reposts_count'];
                    $data[] = $row['comments_count'];
                    $data[] = $row['attitudes_count'];
                    $data[] = $row['retweeted_status']['id'];
                    $data[] = $row['retweeted_status']['user']['id'];
                    $data[] = $row['retweeted_status']['user']['screen_name'];
                    $data[] = "<a href='http://weibo.com/{$row['user']['id']}/{$row['bid']}' target='_blank'>http://weibo.com/{$row['user']['id']}/{$row['bid']}</a>";
                    $data[] = $row['thumbnail_pic'];
                    $data[] = $row['bmiddle_pic'];
                    $data[] = $row['original_pic'];
                    $data[] = $row['addtime'];
                    $data[] = $row['source_myid'];
                    $data[] = $row['user']['id'];
                    $data[] = empty($row['user']['profile_url'])? '': "<a href='http://weibo.com{$row['user']['profile_url']}' target='_blank'>http://weibo.com{$row['user']['profile_url']}</a>";
                    $data[] = $row['user']['screen_name'];
                    $data[] = $row['user']['description'];
                    $data[] = $row['user']['profile_image_url'];
                    $data[] = $row['user']['followers_count'];
                    $data[] = $row['user']['fansNum'];
                    $data[] = $row['user']['verified'];
                    $this->table->add_row($data);
                }
                echo $this->table->generate();
            }
        }

    }
}
